﻿using System;
using System.Collections.Generic;
using System.Text;

namespace yeqw.FrameWork
{
    public class SearchWordHelper
    {


        public static string GetSearchWord(string strKeyField, string strKeyword, string sqlOrderBy)
        {
            return GetSearchWord(strKeyField, strKeyword, sqlOrderBy, ' ');
        }
       /// <summary>
        /// 实现类似于baidu的那种用空格键的分词查询
       /// </summary>
       /// <param name="strKeyField">条件字段</param>
        /// <param name="strKeyword">搜索的关键字</param>
        /// /// <param name="sqlOrderBy">搜索的排序</param>
       /// <param name="splitKey">分割符</param>
       /// <returns></returns>
        public static  string GetSearchWord(string strKeyField, string strKeyword, string sqlOrderBy,char splitKey)
        {
            StringBuilder strCondition = new StringBuilder();
            StringBuilder strwhereList = new StringBuilder();
             string[] tmplist = strKeyword.Split(splitKey);
           
            foreach (string tmp in tmplist)
            {
                strwhereList.AppendFormat("{0} LIKE '%{1}%' or ", strKeyField, tmp);
            }

            strCondition.AppendFormat("({0})", strwhereList.ToString().TrimEnd("or ".ToCharArray()));

            if (!string.IsNullOrEmpty(sqlOrderBy))
            {
                strCondition.AppendFormat(sqlOrderBy);
            }

            return strCondition.ToString();
        }
    }
}
